﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;
using System.Data.SqlClient;

namespace StockDataGeter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string[] tradeNOs = new string[] { "A", "B", "C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M" };

            string[] tradeNOs = new string[] { "M" };

            foreach (string tradeNO in tradeNOs)
            {
                string strWebContent = Tool.GetContentFromUrll("http://www.sse.com.cn/sseportal/webapp/datapresent/SSEQueryHangyeInfoAct?CSRCCODE=" + tradeNO + "&submit0=Go");
                WebBrowser webb1 = new WebBrowser();
                webb1.Navigate("about:blank");
                HtmlDocument htmldoc1 = webb1.Document.OpenNew(true);
                WebBrowser webb2 = new WebBrowser();
                webb2.Navigate("about:blank");
                HtmlDocument htmldoc2 = webb2.Document.OpenNew(true);

                int start1 = strWebContent.IndexOf("<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" bgcolor=\"#337fb2\">");
                int end1 = strWebContent.IndexOf("</table>", start1);
                string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
                htmldoc1.Write(content1);

                int start2 = strWebContent.IndexOf("<span class=\"table3\">查询行业");
                int end2 = strWebContent.IndexOf("</span>", start2);
                string content2 = strWebContent.Substring(start2, end2 - start2 + 6);
                htmldoc2.Write(content2);

                string trade = htmldoc2.GetElementsByTagName("SPAN")[0].InnerText.Remove(0, 5);

                HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");
                for (int i = 1; i < trs.Count; i++)
                {
                    string no = trs[i].GetElementsByTagName("TD")[0].GetElementsByTagName("A")[0].InnerText;

                    string strWebContent2 = Tool.GetContentFromUrll(String.Format("http://www.sse.com.cn/sseportal/webapp/datapresent/SSEQueryListCmpAct?reportName=QueryListCmpRpt&REPORTTYPE=GSZC&PRODUCTID=600180&COMPANY_CODE={0}", no));

                    WebBrowser webb3 = new WebBrowser();
                    webb3.Navigate("about:blank");
                    HtmlDocument htmldoc3 = webb3.Document.OpenNew(true);

                    int start3 = strWebContent2.IndexOf("<table width=\"100%\" align=\"center\" bgcolor=\"#FFFFFF\" class=\"content\">");
                    int end3 = strWebContent2.IndexOf("</table>", start3);
                    string content3 = strWebContent2.Substring(start3, end3 - start3 + 8);
                    htmldoc3.Write(content3);

                    string jianchengString = htmldoc3.GetElementsByTagName("TR")[4].GetElementsByTagName("TD")[1].InnerText;
                    string jianCheng = jianchengString.Remove(jianchengString.IndexOf("/"), jianchengString.Length - jianchengString.IndexOf("/"));

                    string quanChengString = htmldoc3.GetElementsByTagName("TR")[5].GetElementsByTagName("TD")[1].InnerText;
                    string quanCheng = quanChengString.Remove(quanChengString.IndexOf("\r\n"), quanChengString.Length - quanChengString.IndexOf("\r\n"));

                    string address = htmldoc3.GetElementsByTagName("TR")[7].GetElementsByTagName("TD")[1].InnerText;

                    int start4 = strWebContent2.IndexOf("<table width=\"100%\" align=\"center\" bgcolor=\"#FFFFFF\" class=\"content\">", end3);
                    int end4 = strWebContent2.IndexOf("</table>", start4);
                    string content4 = strWebContent2.Substring(start4, end4 - start4 + 8);
                    WebBrowser webb4 = new WebBrowser();
                    webb4.Navigate("about:blank");
                    HtmlDocument htmldoc4 = webb4.Document.OpenNew(true);
                    htmldoc4.Write(content4);

                    string province = htmldoc4.GetElementsByTagName("TR")[7].GetElementsByTagName("TD")[1].InnerText;

                    InsertStock(no, jianCheng, quanCheng, province, trade, address, "shanghai");
                }
            }

        }

        private void InsertStock(string stockNO, string name, string companyName, string province, string trade, string address, string location)
        {
            SqlConnection cn = new SqlConnection("Server=(local);uid=sa;pwd=sa;database=JYStockAnalyser");
            string sql = String.Format("INSERT t_stocks([no], [name], companyName, province, trade, address, location) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')", stockNO, name, companyName, province, trade, address, location);
            SqlCommand cmd = new SqlCommand(sql, cn);
            cn.Open();
            cmd.ExecuteNonQuery();
            cn.Close();
        }        

        private void button2_Click(object sender, EventArgs e)
        {
            StreamReader sr = new StreamReader(@"d:/2222.htm");
            string strWebContent = sr.ReadToEnd();
            sr.Close();

            WebBrowser webb1 = new WebBrowser();
            webb1.Navigate("about:blank");
            HtmlDocument htmldoc1 = webb1.Document.OpenNew(true);

            int start1 = strWebContent.IndexOf("<table x:str border=0 cellpadding=0 cellspacing=0 width=2352");
            int end1 = strWebContent.IndexOf("</table>", start1);
            string content1 = strWebContent.Substring(start1, end1 - start1 + 8);
            htmldoc1.Write(content1);

            HtmlElementCollection trs = htmldoc1.GetElementsByTagName("TR");

            for (int i = 1; i < trs.Count; i++)
            {
                HtmlElementCollection tds = trs[i].GetElementsByTagName("TD");

                string no = tds[0].InnerText;
                string name = tds[6].InnerText;
                string companyName = tds[2].InnerText;
                string address = tds[4].InnerText;
                string p = tds[16].InnerText;
                string trade = tds[18].InnerText;

                InsertStock(no, name, companyName, p, trade, address, "shengzhen");
            }
        }
    }
}
